#include "ToolBarMeasure.h"
#include "DisplayWidget.h"
#include <QAction>
#include <QIcon>

ToolBarMeasure::ToolBarMeasure(QWidget *parent)
    :QToolBar(parent)
{
    setIconSize(QSize(32, 32));
    setObjectName(QStringLiteral("ToolBarMeasure"));
    initAction();
    centerWidget = nullptr;
}

ToolBarMeasure::ToolBarMeasure(const QString &title, QWidget *parent)
    :QToolBar(title, parent)
{
    setIconSize(QSize(32, 32));
    setObjectName(QStringLiteral("ToolBarMeasure"));
    initAction();
    centerWidget = nullptr;
}

void ToolBarMeasure::attach(DisplayWidget *widget)
{
    centerWidget = widget;
}

void ToolBarMeasure::initAction()
{
    setWindowTitle("Measurement Toolbar");
    setObjectName(QStringLiteral("ToolBarMeasure"));

    actionSelect = new QAction(this);
    actionSelect->setObjectName(QStringLiteral("actionSelect"));
    actionSelect->setCheckable(true);
    QIcon iconSelect;
    iconSelect.addFile(QStringLiteral(":/measurement/res/select.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionSelect->setIcon(iconSelect);

    actionLine = new QAction(this);
    actionLine->setObjectName(QStringLiteral("actionLine"));
    actionLine->setCheckable(true);
    QIcon iconLine;
    iconLine.addFile(QStringLiteral(":/measurement/res/line.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLine->setIcon(iconLine);

    actionParallel = new QAction(this);
    actionParallel->setObjectName(QStringLiteral("actionParallel"));
    actionParallel->setCheckable(true);
    QIcon iconParallel;
    iconParallel.addFile(QStringLiteral(":/measurement/res/Parallel.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionParallel->setIcon(iconParallel);

    actionPolyline = new QAction(this);
    actionPolyline->setObjectName(QStringLiteral("actionPolyline"));
    actionPolyline->setCheckable(true);
    QIcon icon14;
    icon14.addFile(QStringLiteral(":/measurement/res/polyline.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionPolyline->setIcon(icon14);

    actionRCircle = new QAction(this);
    actionRCircle->setObjectName(QStringLiteral("actionRCircle"));
    actionRCircle->setCheckable(true);
    QIcon icon3;
    icon3.addFile(QStringLiteral(":/measurement/res/RCircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionRCircle->setIcon(icon3);

    actionLCircle = new QAction(this);
    actionLCircle->setObjectName(QStringLiteral("actionLCircle"));
    actionLCircle->setCheckable(true);
    QIcon icon4;
    icon4.addFile(QStringLiteral(":/measurement/res/LCircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLCircle->setIcon(icon4);

    actionPCircle = new QAction(this);
    actionPCircle->setObjectName(QStringLiteral("actionPCircle"));
    actionPCircle->setCheckable(true);
    QIcon icon16;
    icon16.addFile(QStringLiteral(":/measurement/res/pcircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionPCircle->setIcon(icon16);

    actionPolygon = new QAction(this);
    actionPolygon->setObjectName(QStringLiteral("actionPolygon"));
    actionPolygon->setCheckable(true);
    QIcon icon15;
    icon15.addFile(QStringLiteral(":/measurement/res/polygon.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionPolygon->setIcon(icon15);

    actionRect = new QAction(this);
    actionRect->setObjectName(QStringLiteral("actionRect"));
    actionRect->setCheckable(true);
    QIcon icon5;
    icon5.addFile(QStringLiteral(":/measurement/res/rect.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionRect->setIcon(icon5);

    actionCorner = new QAction(this);
    actionCorner->setObjectName(QStringLiteral("actionCorner"));
    actionCorner->setCheckable(true);
    QIcon icon6;
    icon6.addFile(QStringLiteral(":/measurement/res/corner.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionCorner->setIcon(icon6);

    actionDots2Line = new QAction(this);
    actionDots2Line->setObjectName(QStringLiteral("actionDots2Line"));
    actionDots2Line->setCheckable(true);
    QIcon icon17;
    icon17.addFile(QStringLiteral(":/measurement/res/DotsToLine.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionDots2Line->setIcon(icon17);

    addAction(actionSelect);
    addAction(actionLine);
    addAction(actionParallel);
    addAction(actionPolyline);
    addAction(actionRCircle);
    addAction(actionLCircle);
    addAction(actionPCircle);
    addAction(actionRect);
    addAction(actionPolygon);
    addAction(actionCorner);
    addAction(actionDots2Line);

    connect(actionSelect, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionLine, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionParallel, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionLCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionRCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionRect, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionCorner, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionPolygon, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionPolyline, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionPCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionDots2Line, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
}

void ToolBarMeasure::upateDrawType(bool on)
{
    Q_UNUSED(on);
    if(centerWidget == nullptr) return;
    QAction * action = (QAction *) sender();
    uncheckAll();

    action->setChecked(true);
    if( action == actionSelect )
    {
        centerWidget->setDrawType(Element::NONE);
    }
    else if(action == actionLine)
    {
        centerWidget->setDrawType(Element::LINE);
    }
    else if(action == actionParallel)
    {
        centerWidget->setDrawType(Element::PARALLEL);
    }
    else if(action == actionLCircle)
    {
        centerWidget->setDrawType(Element::LCIRCLE);
    }
    else if(action == actionRCircle)
    {
        centerWidget->setDrawType(Element::RCIRCLE);
    }
    else if(action == actionRect)
    {
        centerWidget->setDrawType(Element::RECTAGLE);
    }
    else if(action == actionCorner)
    {
        centerWidget->setDrawType(Element::CORNER);
    }
    else if(action == actionPolyline)
    {
        centerWidget->setDrawType(Element::POLYLINE);
    }
    else if(action == actionPolygon)
    {
        centerWidget->setDrawType(Element::POLYGON);
    }
    else if(action == actionPCircle)
    {
        centerWidget->setDrawType(Element::PCIRCLE);
    }
    else if(action == actionDots2Line)
    {
        centerWidget->setDrawType(Element::DOTSTOLINE);
    }

}

void ToolBarMeasure::uncheckAll()
{
    actionSelect->setChecked(false);
    actionLine->setChecked(false);
    actionParallel->setChecked(false);
    actionLCircle->setChecked(false);
    actionRCircle->setChecked(false);
    actionRect->setChecked(false);
    actionCorner->setChecked(false);
    actionPolygon->setChecked(false);
    actionPolyline->setChecked(false);
    actionPCircle->setChecked(false);
    actionDots2Line->setChecked(false);
}
